import { requestGet, requestPost } from '@/api/system/common'
/**
 * 列表模版设置
 */
export const setting = {
  props: {
    data: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      controlDataList: [],
      gxfList: [],
      jxx: [],
      entityTemp: [],
      openType: [
        { id: 1, value: 1, label: '地址参数' },
        { id: 2, value: 2, label: '地址传参' },
        { id: 3, value: 3, label: '详情模版' }
      ],
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      types: [
        { id: 0, value: 'custom', label: '自定义地址' },
        { id: 1, value: 'system', label: '默认地址' }
      ],
      orders: [
        { id: 0, value: 'DESC', label: '降序' },
        { id: 1, value: 'ASC', label: '升序' }
      ],
      dateType: [
        {
          id: 0, value: 'yyyy-MM-dd HH:mm:ss', label: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          id: 1, value: 'yyyy/MM/dd HH:mm:ss', label: 'yyyy/MM/dd HH:mm:ss'
        },
        {
          id: 2, value: 'yyyyMMddHHmmss', label: 'yyyyMMddHHmmss'
        },
        {
          id: 3, value: 'yyyy年MM月dd日 HH:mm:ss', label: 'yyyy年MM月dd日 HH:mm:ss'
        }
      ],
      form: {
        isOpenEntity: true, // 是否打开详情页
        isOpenMore: true, // 是否显示更多
        isRefresh: false, // 是否定时刷新
        isCustomShort: false, // 是否自定义排序
        refreshTime: 20, // 定时刷新时间单位秒
        openMoreType: '', // 更多打开方式，外部链接，还是更多页面
        openMoreUrl: '', // 更多显示地址
        isFixTitle: false, // 是否固定标题
        entityTitleCode: '', // 标题关联的字段
        entityTitle: '', // 固定标题值
        size: 5, // 每页显示几条
        entityTemplateId: null, // 详情页模版编号
        data: [{}], // 数据显示项配置
        shortColumn: [], // 排序字段及排序方式
        query: [], // 查询条件设置
        entityParams: [], // 详情参数设置
        params: [], // 绑定详情打方式为地址参数时的数据绑定
        entityOpenType: null, // 详情页打开方式
        entityUrl: '', // 详情地址
        entityTemp: null
      }
    }
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (newName.sjlx === 1) {
            this.initDataJxx()
          } else if (newName.sjlx === 2) {
            this.initSystemJxx()
          }
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx)
            this.$nextTick(() => {
              if (this.form.isOpenEntity === undefined) {
                // this.form.isOpenEntity = true
                this.$set(this.form, 'isOpenEntity', true)
              }
              if (this.form.isOpenMore === undefined) {
                // this.form.isOpenMore = true
                this.$set(this.form, 'isOpenMore', true)
              }
              if (this.form.isRefresh === undefined) {
                // this.form.isRefresh = false
                this.$set(this.form, 'isRefresh', true)
              }
              if (this.form.refreshTime === undefined) {
                // this.form.refreshTime = 20
                this.$set(this.form, 'refreshTime', 20)
              }
              if (this.form.openMoreType === undefined) {
                // this.form.openMoreType = ''
                this.$set(this.form, 'openMoreType', '')
              }
              if (this.form.openMoreUrl === undefined) {
                // this.form.openMoreUrl = ''
                this.$set(this.form, 'openMoreUrl', '')
              }
              if (this.form.isFixTitle === undefined) {
                // this.form.isFixTitle = false
                this.$set(this.form, 'isFixTitle', false)
              }
              if (this.form.entityTitleCode === undefined) {
                // this.form.entityTitleCode = ''
                this.$set(this.form, 'entityTitleCode', '')
              }
              if (this.form.entityTitle === undefined) {
                // this.form.entityTitle = ''
                this.$set(this.form, 'entityTitle', '')
              }
              if (this.form.size === undefined) {
                // this.form.size = 5
                this.$set(this.form, 'size', 5)
              }
              if (this.form.entityTemplateId === undefined) {
                // this.form.entityTemplateId = null
                this.$set(this.form, 'entityTemplateId', null)
              }
              if (this.form.entityOpenType === undefined) {
                // this.form.entityOpenType = null
                this.$set(this.form, 'entityOpenType', null)
              }
              if (this.form.entityUrl === undefined) {
                // this.form.entityUrl = ''
                this.$set(this.form, 'entityUrl', null)
              }
              if (this.form.entityTemp === undefined) {
                // this.form.entityTemp = null
                this.$set(this.form, 'entityTemp', null)
              }
              if (this.form.data === undefined) {
                this.$set(this.form, 'data', [{}])
              }
              if (this.form.shortColumn === undefined) {
                // this.form.shortColumn = []
                this.$set(this.form, 'shortColumn', [])
              }
              if (this.form.query === undefined) {
                // this.form.query = []
                this.$set(this.form, 'query', [])
              }
              if (this.form.entityParams === undefined) {
                // this.form.entityParams = []
                this.$set(this.form, 'entityParams', [])
              }
              if (this.form.params === undefined || this.form.params === null) {
                // this.form.params = []
                this.$set(this.form, 'params', [])
              }
            })
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    // 查询文本类型
    this.initControl()
    // 查询关系符号
    this.initGxf()
    // 初始化全部详情模版
    this.initAllTemp()
  },
  methods: {
    doAdd() {
      const item = { label: '', column: '', width: null }
      this.form.data.push(item)
    },
    doAddQuery() {
      const item = { label: '', column: '', type: null, ysf: null }
      this.form.query.push(item)
    },
    doAddOrder() {
      const item = { pxx: '', order: '', id: null }
      this.form.shortColumn.push(item)
    },
    doAddParams() {
      const item = { code: '', column: '' }
      this.form.params.push(item)
    },
    doDeleteQuery(item) {
      const index = this.form.query.indexOf(item)
      if (index !== -1) {
        this.form.query.splice(index, 1)
      }
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item)
      if (index !== -1) {
        this.form.data.splice(index, 1)
      }
    },
    removeOrder(item) {
      const index = this.form.shortColumn.indexOf(item)
      if (index !== -1) {
        this.form.shortColumn.splice(index, 1)
      }
    },
    removeParams(item) {
      const index = this.form.params.indexOf(item)
      if (index !== -1) {
        this.form.params.splice(index, 1)
      }
    },
    save() {
      this.loading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form)
          requestPost('rest/api/temp/v1/column/manager/update/entity', this.data)
            .then(res => {
              this.close()
            }).catch(() => {
              this.loading = false
            })
        } else {
          this.loading = false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    // 查询文本类型
    initControl() {
      requestGet('rest/api/template/v1/config/type/control', {})
        .then(res => {
          const { data } = res
          this.controlDataList = data
        })
    },
    // 查询关系符
    initGxf() {
      requestGet('rest/api/template/v1/config/type/gxf', {})
        .then(res => {
          const { data } = res
          this.gxfList = data
        })
    },
    // 查询数据库数据项
    initDataJxx() {
      requestGet('rest/api/template/v1/analysis/data/source', { id: this.data.sjx })
        .then(res => {
          const { data } = res
          this.jxx = data
        })
    },
    // 查询系统数据项
    initSystemJxx() {
      requestGet('rest/api/template/v1/analysis/data/system', { id: this.data.sjx })
        .then(res => {
          const { data } = res
          this.jxx = data
        })
    },
    // 查询全部系统详情模版
    initAllTemp() {
      requestGet('rest/api/temp/v1/entity/temp/blurry', {})
        .then(res => {
          const { data } = res
          this.entityTemp = data.data
        }).catch(() => {
          this.entityTemp = []
        })
    },
    entityChange(data) {
      const entity = this.entityTemp.filter(item => {
        if (item.id === data) {
          return item
        }
      })
      if (entity !== undefined && entity !== null && entity.length > 0) {
        const param = entity[0].params
        this.form.entityTemp = entity[0]
        this.form.entityParams = []
        if (undefined !== param && param !== null) {
          const newParams = JSON.parse(param).map(item => {
            item.test = false
            item.value = ''
            return item
          })
          newParams.forEach(item => {
            this.form.entityParams.push(item)
          })
        } else {
          this.form.entityParams = []
        }
      }
    }
  }
}

/**
 * 统计组件相关设置
 * @type {{}}
 */
export const statistics = {
  props: {
    data: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      dataTypes: [],
      tempList: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      openType: [
        { id: 0, value: 1, label: '指定模版' },
        { id: 1, value: 2, label: '固定地址' }
      ],
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      form: {
        rows: 1,
        data: []
      }
    }
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx)
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
  // 解决火狐拖拽搜索的问题
    document.body.ondrop = function(event) {
      event.preventDefault()
      event.stopPropagation()
    }
    this.initTemp()
  },
  methods: {
    doAdd() {
      const item = {
        label: '',
        sjly: {},
        temp: null,
        px: 0,
        backColorA: '',
        isOpenMore: false,
        openType: null,
        backColorB: '',
        titleFontSize: 16,
        fonSize: 18,
        titleColor: '',
        tb: '',
        openUrl: ''
      }
      this.form.data.push(item)
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item)
      if (index !== -1) {
        this.form.data.splice(index, 1)
      }
    },
    clone() {
      this.form.data = this.form.data.map((item, index) => {
        item.px = index + 1
        return item
      })
    },
    initTemp() {
      requestGet('rest/api/temp/v1/column/manager/blurry',
        { mbdl: 'TabsTemplate', pt: 'app' }).then(res => {
        const { data } = res
        this.tempList = data.data
      }).catch(() => {
        this.tempList = []
      })
    },
    save() {
      this.loading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form)
          requestPost('rest/api/temp/v1/column/manager/update/entity', this.data).then(res => {
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.loading = false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
